//
//  StoreAllGoodsListViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/22.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "StoreAllGoodsListViewModel.h"
#import "InSaleGoodsTableViewCell.h"
#import "AllGoodsTopView.h"
#import "StroeAllGoodsModel.h"
#import "TableViewPleaseholdView.h"

@interface StoreAllGoodsListViewModel ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView * self_tableView;
}
@property(nonatomic,strong) NSMutableArray * goodsDataSource;
@end

@implementation StoreAllGoodsListViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        self.goodsDataSource = [NSMutableArray arrayWithCapacity:0];
        tableView.y = tableView.y+kHeightProportion_In_iPhoneTriploid(44);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(88);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(136);
        [superView addSubview:tableView];
        self_tableView = tableView;
        [self createTopView:superView];
        [self requestAllGoodsData];
    }
    return self;
}

-(void)createTopView:(UIView *)superView{
    AllGoodsTopView * allGoodsView = [[AllGoodsTopView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) inSuperView:superView];
}

-(void)noGoodsDataShowView{
    if (self.goodsDataSource.count == 0) {
        TableViewPleaseholdView * noDataModelView = [[TableViewPleaseholdView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(86), KScreenWidth, kHeightProportion_In_iPhoneTriploid(200)) thePicStr:@"icon_quesheng_znwuneirong" theTosStr:@"暂无内容" theMiaoStr:@"" theButStr:@"" theButWidth:0];
        [self showNoDataImageView:noDataModelView];
    }
}

#pragma mark -- 数据请求
-(void)requestAllGoodsData{
    @weakify(self)
    _goods_recommendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        RACSignal * store_Signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSDictionary * storeDic = input;
            if ([[input objectForKey:@"page"] integerValue] == 1) {
                [self.goodsDataSource removeAllObjects];
            }
            NSString * apiStr;
            if (self.accountType == AccountType_Ordinary) {
                apiStr = kStoreproductDetails;
            }else{
                apiStr = kFreeStoreproductDetails;
            }
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,apiStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:storeDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [self noGoodsDataShowView];
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        
        return [store_Signal map:^id _Nullable(id  _Nullable value) {
            StroeAllGoodsModel * storeGoodsModel = [StroeAllGoodsModel yy_modelWithJSON:value];
            if (storeGoodsModel.code == 0) {
            [self.goodsDataSource addObjectsFromArray:storeGoodsModel.data.infos];
                [self noGoodsDataShowView];
                [self->self_tableView reloadData];
            }
            return storeGoodsModel;
        }];
    }];
}
#pragma mark ----UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.goodsDataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InSaleGoodsTableViewCell * cell = [InSaleGoodsTableViewCell cellWithTableView:tableView];
    cell.storeInfosModel = self.goodsDataSource[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(chickSelectRow:)]) {
        [self.delegate chickSelectRow:indexPath.row];
    }
}
@end
